package com.basker.pisces.auth.domain.entity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.basker.pisces.auth.domain.entity.command.AuthUserRegistCommand;
import com.basker.pisces.auth.domain.entity.command.ChangePasswordCommand;
import com.basker.pisces.auth.orm.AuthUser;
import com.basker.pisces.domain.command.meta.Command;
import com.basker.pisces.domain.command.meta.SaveCommand;
import com.basker.pisces.domain.entity.annotation.EntityMeta;
import com.basker.pisces.domain.entity.meta.BeanEntity;
import com.basker.pisces.domain.entity.meta.Entity;
import com.basker.pisces.domain.property.PropertyFactory;
import com.basker.pisces.domain.property.meta.MobileNoProperty;
import com.basker.pisces.domain.property.meta.PasswordProperty;
import com.basker.pisces.domain.property.meta.Property;
import com.basker.pisces.domain.property.meta.TextProperty;
import com.basker.pisces.domain.validation.meta.UniquePropertyValidation;
import com.basker.pisces.domain.validation.meta.Validation;

//gitee.com/albafika/pisces

@EntityMeta
public class AuthUserEntity extends BeanEntity<AuthUser> {

    private AuthUserRegistCommand registCommand;
    private ChangePasswordCommand changePasswordCommand;

    private List<UniquePropertyValidation> uniquePropertyValidations = new ArrayList<>();

    public AuthUserEntity() {
        this.setEnableDefaultDisplayNameRule(true);
    }

    public ChangePasswordCommand getChangePasswordCommand() {
        return changePasswordCommand;
    }

    public AuthUserRegistCommand getRegistCommand() {
        return registCommand;
    }

    @Override
    protected List<Command> createCommands() {
        this.registCommand = new AuthUserRegistCommand();
        this.registCommand.getValidations().addAll(this.uniquePropertyValidations);

        this.changePasswordCommand = new ChangePasswordCommand();

        return Arrays.asList(this.registCommand, this.changePasswordCommand);
    }

    @Override
    protected List<Property<?>> createProperties(Entity entity) {
        TextProperty nameProperty = PropertyFactory.create(entity, TextProperty.class, "Name");
        nameProperty.setRequired(true);
        nameProperty.setMaxLength(10);

        MobileNoProperty mobileNoProperty = PropertyFactory.create(entity, MobileNoProperty.class, "CellPhone");

        PasswordProperty passwordProperty = PropertyFactory.create(entity, PasswordProperty.class, "Password");
        passwordProperty.setRequired(true);

        return Arrays.asList(nameProperty, mobileNoProperty, passwordProperty);
    }

    @Override
    protected SaveCommand createSaveCommand() {
        SaveCommand saveCommand = super.createSaveCommand();
        if (saveCommand != null) {
            saveCommand.getValidations().addAll(this.uniquePropertyValidations);
        }

        return saveCommand;
    }

    @Override
    protected List<Validation> createValidations() {
        this.uniquePropertyValidations.add(new UniquePropertyValidation("NameUnique", "Name"));
        this.uniquePropertyValidations.add(new UniquePropertyValidation("CellPhoneUnique", "CellPhone"));

        return Arrays
                .asList(this.uniquePropertyValidations.toArray(new Validation[this.uniquePropertyValidations.size()]));
    }

}
